﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;

namespace mfe_proj.Forms
{
    public partial class EssayForm : BaseDetailForm
    {
        public ItemEssay essay = null;
        string _ItemGuidID;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }

        public EssayForm()
        {
            InitializeComponent();
        }

        public override string ValidateData()
        {
            return base.ValidateData();
        }

        public override bool SaveData(string itemGuidID)
        {
            bool retval = false;
            try
            {
                //general
                if (essay == null)
                {
                    essay = new ItemEssay();
                }
                essay.ItemGuidID = itemGuidID;
                essay.OriginalAuthor = txtOriginalAuthor.Text.Trim();
                if (cbPublishType.SelectedIndex > -1)
                    essay.PublishTypeID = int.Parse(cbPublishType.SelectedValue.ToString());
                essay.Author = txtAuthor.Text.Trim();
                essay.AppearAt = txtAppearAt.Text.Trim();
                essay.Note = txtNote.Text.Trim();

                if (ymdPublished.Year.HasValue)
                    essay.PublishedYear = ymdPublished.Year.Value;
                if (ymdPublished.Month.HasValue)
                    essay.PublishedMonth = ymdPublished.Month.Value;
                if (ymdPublished.Day.HasValue)
                    essay.PublishedDay = ymdPublished.Day.Value;
                //save
                retval = (essay.ItemEssayID == 0) ? essay.Save() : essay.Update();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return retval;
        }

        public void LoadData()
        {
            LoadPublishType();

            //edit
            if (ItemGuidID != null && ItemGuidID != string.Empty)
            {
                //general
                essay = new ItemEssay(new Guid(ItemGuidID));
                ymdPublished.Year = (int?)essay.PublishedYear;
                ymdPublished.Month = (int?)essay.PublishedMonth;
                ymdPublished.Day = (int?)essay.PublishedDay;
                txtOriginalAuthor.Text = essay.OriginalAuthor;
                txtAuthor.Text = essay.Author;
                txtAppearAt.Text = essay.AppearAt;
                txtNote.Text = essay.Note;

                if (essay.PublishTypeID > 0)
                    cbPublishType.SelectedValue = essay.PublishTypeID;
            }
        }

        void LoadPublishType()
        {
            if (Program.PublishTypeCollection == null) Program.PublishTypeCollection = PublishType.PublishTypeCollection(null, null);

            cbPublishType.DataSource = Program.PublishTypeCollection;
            cbPublishType.DisplayMember = "PublishTypeName";
            cbPublishType.ValueMember = "PublishTypeID";
            cbPublishType.SelectedIndex = -1;
        }

        public Panel DockablePanel
        {
            get { return panel1; }
            set { panel1 = value; }
        }

        private void EssayForm_Load(object sender, EventArgs e)
        {

        }
    }
}
